﻿using Acme.SmartWMS.Common;
using Acme.SmartWMS.RBAC.Roles.DTO.UpdateDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.SmartWMS.RBAC.Roles
{
    public class RoleAppService : ApplicationService, IRoleAppService
    {
        /// <summary>
        /// 角色接口
        /// </summary>
        private readonly IRepository<Role, int> _rolerepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="accountrepository"></param>
        public RoleAppService(IRepository<Role, int> rolerepository)
        {
            _rolerepository = rolerepository;
        }


        /// <summary>
        /// 角色显示（分页）
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="limit">页容量</param>
        /// <param name="roleName">用户名</param>
        /// <returns></returns>
        public async Task<PagedResult> Display(int page, int limit, string? roleName)
        {
            var predicate = PredicateBuilder.New<Role>(true);
            predicate = predicate.And(m => m.RoleState == 1);
            if (!string.IsNullOrEmpty(roleName))
            {
                predicate = predicate.And(m => m.RoleName.Contains(roleName));
            }

            var accountList = await _rolerepository.GetListAsync(predicate);

            PagedResult pagedResult = new PagedResult();
            pagedResult.CurrentPage = accountList.Count();
            pagedResult.Queryable = accountList.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return pagedResult;

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Delete(int id)
        {
            var accountDelete = _rolerepository.FindAsync(id).Result;
            accountDelete.RoleState = 0;
            await _rolerepository.UpdateAsync(accountDelete);
            return 1;
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回受影响行数</returns>
        public async Task<Role> Backfill(int id)
        {
            var accountList = await _rolerepository.FindAsync(id);
            return accountList;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="account">用户实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Add(Role role)
        {
            var verify = await _rolerepository.CountAsync(m => m.RoleName == role.RoleName);
            if (verify > 0)
            {
                return -1;
            }

            var accountAdd = await _rolerepository.InsertAsync(role);
            return 1;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="account">修改DTO</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Update(UpdateRoleDto role)
        {
            var verify = await _rolerepository.CountAsync(m => m.RoleName == role.RoleName && m.Id != role.Id);
            if (verify > 0)
            {
                return -1;
            }

            var roleList = role.MapTo<Role>();
            await _rolerepository.UpdateAsync(roleList);
            return 1;

        }

    }
}
